﻿using LatteCoffee.coffeemodel;
using Newtonsoft.Json;
using System;
using System.IO.Ports;
using System.Text;
using System.Threading;

namespace LatteCoffee
{
    public class IOMsgShow
    {
        private static IOMsgShow instance;
        private IOMsgShow() { }

        public static IOMsgShow Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new IOMsgShow();
                }
                return instance;
            }
        }
        public void show(int code, string msg)
        {
            try
            {
                Form1.AllBol = false;
                Thread.Sleep(100);
                switch (msg)
                {
                    case "DO1":
                        if (code == 1)
                        {
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction2_end);
                        }
                        else
                        {
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction2_end);
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction2_start);
                        }
                        break;
                    case "DO2":
                        if (code == 1)
                        {
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction2_end);
                        }
                        else
                        {
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction2_end);
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_start);
                        }
                        break;
                    case "DO3":
                        ClientList listDO1 = new ClientList();
                        MessageClientList coffeeDO1 = MessageClientList.Instance;
                        coffeeDO1.Name = msg;
                        coffeeDO1.type = 1;
                        listDO1.message = coffeeDO1;
                        listDO1.code = 0;
                        Form1.socketClient.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(listDO1)));
                      //  DiaitalBool.Instance.UPCupBtn1_Sel_Bol = true;
                        Thread.Sleep(100);
                        Form1.AllBol = true;
                        Thread.Sleep(1000);
                        while (!Form1.CupBol1)
                        {
                            Thread.Sleep(10);
                        }
                        code = 1;
                        break;
                }
                Thread.Sleep(100);
                Form1.AllBol = true;
                ClientList clientList = new ClientList();
                MessageClientList coffeelist = MessageClientList.Instance;
                coffeelist.Name = msg;
                coffeelist.type = code == 1 ? 0 : 1;
                clientList.message = coffeelist;
                clientList.code = 0;
                Form1.socketClient.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        ///   状态  0 关状态  1 开状态 2 已开机 3 上电 4 下电 5 已使能 6 下使能 7 已运行 8 继续运行 9 已报警 10 已暂停  11已停止 ,12 机械臂状态查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="Type"></param>
        public void RobotShow(string name, int Type)
        {
            try
            {
                if (name == "Reload_L")
                {
                    switch (Type)
                    {
                        case 3:
                            jakaAPI.power_on(ref RobotOperate.handle1);
                            break;
                        case 4:
                            jakaAPI.power_off(ref RobotOperate.handle1);
                            break;
                        case 5:
                            jakaAPI.enable_robot(ref RobotOperate.handle1);
                            break;
                        case 6:
                            jakaAPI.disable_robot(ref RobotOperate.handle1);
                            break;
                        case 7:
                            jakaAPI.program_run(ref RobotOperate.handle1);
                            break;
                        case 8:
                            jakaAPI.program_resume(ref RobotOperate.handle1);
                            break;
                        case 9:
                            jakaAPI.clear_error(ref RobotOperate.handle1);
                            break;
                        case 10:
                            jakaAPI.program_pause(ref RobotOperate.handle1);
                            break;
                        case 11:
                            jakaAPI.program_abort(ref RobotOperate.handle1);
                            break;
                    }
                    RobotOperate.Robot_L_Show();
                }
                else
                {
                    switch (Type)
                    {
                        case 3:
                            jakaAPI.power_on(ref RobotOperate.handle2);
                            break;
                        case 4:
                            jakaAPI.power_off(ref RobotOperate.handle2);
                            break;
                        case 5:
                            jakaAPI.enable_robot(ref RobotOperate.handle2);
                            break;
                        case 6:
                            jakaAPI.disable_robot(ref RobotOperate.handle2);
                            break;
                        case 7:
                            jakaAPI.program_run(ref RobotOperate.handle2);
                            break;
                        case 8:
                            jakaAPI.program_resume(ref RobotOperate.handle2);
                            break;
                        case 9:
                            jakaAPI.clear_error(ref RobotOperate.handle1);
                            break;
                        case 10:
                            jakaAPI.program_pause(ref RobotOperate.handle2);
                            break;
                        case 11:
                            jakaAPI.program_abort(ref RobotOperate.handle2);
                            break;
                    }
                    RobotOperate.Robot_R_Show();
                }
            }
            catch (Exception)
            {
            }
        }
    }
}